package com.lyn.solitude.plugin.support

import android.annotation.SuppressLint
import android.app.Application
import android.content.Context
import android.content.Intent
import android.content.res.AssetManager
import android.content.res.Resources
import android.graphics.drawable.Drawable
import android.os.Bundle
import android.os.Handler
import android.os.Looper
import android.os.RemoteException
import android.view.ContextMenu
import android.view.ContextMenu.ContextMenuInfo
import android.view.LayoutInflater
import android.view.Menu
import android.view.MenuInflater
import android.view.MenuItem
import android.view.View
import android.view.Window
import androidx.annotation.IdRes
import androidx.appcompat.app.ActionBar
import androidx.appcompat.app.AppCompatActivity
import androidx.core.content.ContextCompat
import androidx.fragment.app.FragmentManager
import androidx.lifecycle.Lifecycle
import androidx.lifecycle.LifecycleCoroutineScope
import androidx.lifecycle.lifecycleScope
import com.lyn.solitude.api.PluginHostAidl
import com.lyn.solitude.api.PluginLifecycleApi
import com.lyn.solitude.entity.Plugin

/**
 * 插件 支持
 *
 * @author longyn 2024/4/20
 * @version 1.0.0
 */
open class PluginSupport : PluginLifecycleApi {
    val handler = Handler(Looper.getMainLooper())
    private lateinit var activity: AppCompatActivity
    private lateinit var plugin: Plugin
    private lateinit var pluginHostAidl: PluginHostAidl
    override fun onPluginHostAidl(pluginHostAidl: PluginHostAidl) {
        this.pluginHostAidl = pluginHostAidl
    }

    override fun getPluginHostAidl(): PluginHostAidl {
        return this.pluginHostAidl
    }

    override fun onPlugin(plugin: Plugin) {
        this.plugin = plugin
    }

    override fun getPlugin(): Plugin {
        return plugin
    }


    override fun getActivity(): AppCompatActivity {
        return activity
    }

    override fun getContext(): Context {
        return activity
    }

    override fun onActivity(activity: AppCompatActivity) {
        this.activity = activity
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        val supportActionBar = getSupportActionBar()
        supportActionBar.setDisplayHomeAsUpEnabled(true)
        supportActionBar.title = plugin.name
    }

    override fun setContentView(layoutId: Int) {
        activity.setContentView(layoutId)
    }

    override fun setContentView(contentView: View?) {
        activity.setContentView(contentView)
    }

    override fun getIntent(): Intent {
        return activity.intent
    }

    override fun getAssets(): AssetManager {
        return activity.assets
    }

    override fun getClassLoader(): ClassLoader {
        return activity.classLoader
    }

    override fun getResources(): Resources {
        return activity.getResources()
    }

    override fun getString(resId: Int): String {
        return activity.getString(resId)
    }

    override fun getColor(resId: Int): Int {
        return activity.getColor(resId)
    }

    @SuppressLint("UseCompatLoadingForDrawables")
    override fun getDrawable(redId: Int): Drawable {
        return activity.getDrawable(redId)!!
    }

    override fun getLayoutInflater(): LayoutInflater {
        return activity.layoutInflater
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        if (item.itemId == android.R.id.home) {
            finish()
            return true
        }
        return false
    }

    override fun onMenuOpened(featureId: Int, menu: Menu): Boolean {
        return true
    }

    override fun onNewIntent(intent: Intent?) {}
    override fun onStart() {}
    override fun onRestart() {}
    override fun onResume() {}
    override fun onPause() {}
    override fun onDestroy() {}
    override fun onRequestPermissionsResult(
        requestCode: Int,
        permissions: Array<String?>,
        grantResults: IntArray
    ) {
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {}
    override fun isNotBackPressed(): Boolean {
        return false
    }

    override fun onBackPressed() {
        activity.onBackPressedDispatcher.onBackPressed()
    }

    override fun getSupportFragmentManager(): FragmentManager {
        return activity.supportFragmentManager
    }

    override fun getApplication(): Application {
        return activity.application
    }

    override fun startPluginActivity(pluginClass: Class<out PluginLifecycleApi?>) {
        try {
            pluginHostAidl.jump(plugin.id, pluginClass.name)
        } catch (e: RemoteException) {
            e.printStackTrace()
        }
    }

    override fun startActivity(intent: Intent?) {
        activity.startActivity(intent)
    }

    override fun startActivity(intent: Intent?, options: Bundle?) {
        activity.startActivity(intent, options)
    }

    override fun startActivityForResult(intent: Intent?, requestCode: Int) {
        activity.startActivityForResult(intent!!, requestCode)
    }

    override fun startActivityForResult(intent: Intent?, requestCode: Int, options: Bundle?) {
        activity.startActivityForResult(intent!!, requestCode, options)
    }


    override fun getSupportActionBar(): ActionBar {
        return activity.supportActionBar!!
    }

    override fun finish() {
        activity.finish()
    }

    override fun <T : View?> findViewById(@IdRes id: Int): T {
        return activity.findViewById(id)
    }

    override fun getWindow(): Window {
        return activity.window
    }

    override fun <T> getSystemService(serviceClass: Class<T>?): T {
        return activity.getSystemService(serviceClass!!)
    }

    override fun getSystemService(name: String?): Any {
        return activity.getSystemService(name!!)
    }

    override fun getMenuInflater(): MenuInflater {
        return activity.menuInflater
    }

    override fun getLifecycle(): Lifecycle {
        return activity.lifecycle
    }

    override fun lifecycleScope(): LifecycleCoroutineScope {
        return activity.lifecycleScope
    }

    override fun checkSelfPermission(permission: String): Int {
        return ContextCompat.checkSelfPermission(getContext(), permission)
    }

    override fun requestPermissions(permissions: Array<String>, requestCode: Int) {
        getActivity().requestPermissions(permissions, requestCode)
    }

    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        return false
    }

    override fun onCreateContextMenu(menu: ContextMenu?, v: View?, menuInfo: ContextMenuInfo?) {}
}
